<template>
	<view class="flex-col page">
		<view>
			<view :style="{height:statusBarHeight+'px'}"></view>
			<u-navbar title="消息订阅" bgColor="#F5F5F5" @leftClick="$nav(2)">	
			</u-navbar>
		</view>
		
		
		<view class="container">
		    <button @click="subscribeMessage">消息订阅</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bind:'',
				type:''
			};
		},
		computed:{
			statusBarHeight(){
				return this.$store.state.statusBarHeight + 44
			}
		},
		onLoad(op) {
			const scanUrl = decodeURIComponent(op.url);
			const params = this.parseQueryString(scanUrl)
			if('bind' in params && 'type' in params){
				this.bind = params.bind
				this.type = params.type
			}
		},
		methods:{
			parseQueryString(url) {
			  const query = {};
			  const urlParts = url.split('?');
			  
			  if (urlParts.length > 1) {
			    const queryString = urlParts[1];
			    const pairs = queryString.split('&');
			    
			    for (const pair of pairs) {
			      const [key, value] = pair.split('=');
			      query[decodeURIComponent(key)] = decodeURIComponent(value || '');
			    }
			  }
			  
			  return query;
			},
			
			
			subscribeMessage(){
				let that = this;
				uni.requestSubscribeMessage({
					tmplIds: ['aBZXGmimGtn3L5wOMjT0sQ-V-azZrEDdJZXqm_oXybo', 'WbQfA4nSUgjG34uheQcgtM5slsQW5WFZR827Qfb-cxg' ,'2ntUnj6dw1IoZCIXXcjBq4R6mw-LfiLzhqZchvlUOAw'], // 替换为你的模板ID
					success: (res) => {
					  console.log('订阅成功', res);
					  let keys = Object.keys(res).filter(key => res[key] == 'accept')
					  wx.login({
						  success(wc){
							  if(wc.code){
								  uni.request({
								  	url: 'https://crm.scmzhc.com/gCJqIxYPfe.php/Orderpush/addadmin',
									method: 'POST',
								    data: {
										bind : that.bind,
										type : that.type,
										template_id : keys.join(','),
										code : wc.code
									},
									success: (response) => {
										console.log('消息发送成功', response);
										uni.showToast({
											title:'订阅成功'
										})
									},
								    fail: (error) => {
										console.error('消息发送失败', error);
									}
								  })
							  }
						  }
					  })
					  
					  
					   
					   
					},
					fail: (err) => {
					  console.error('订阅失败', err);
					}
				  });
				
			}
		}
	}
</script>

<style lang="scss">
.page{
	width: 100%;
	overflow-y: auto;
	overflow-x: hidden;
	height: 100vh;
}
.container {
  padding: 0px;
}
button {
  background-color: #007aff;
  color: #ffffff;
  padding: 10px 20px;
  border-radius: 5px;
  margin: 50rpx;
}
</style>
